#!/usr/bin/python
# -*- coding: UTF-8 -*-

# #导入form组件
# from django import forms
# from app01 import models
# class EmpForm(forms.Form):  #要继承form组件,要不然他就是普通的一个类,下面的字段是规则(约束)
#     # 定义一个属性,可以用来检验字符串类型
#     name = forms.CharField(min_length=4, label="姓名", error_messages={"min_length": "你太短了", "required": "该字段不能为空!!"})
#     age = forms.IntegerField(label="年龄")
#     salary = forms.DecimalField(label="工资")

from django import forms
from django.core.exceptions import ValidationError, NON_FIELD_ERRORS
from app01 import models


class EmpForm(forms.Form):
    name = forms.CharField(min_length=2, label="姓名", error_messages={"min_length": "用户名太短了", "required": "该字段不能为空!!"})
    age = forms.IntegerField(label="年龄")
    salary = forms.DecimalField(max_digits=5, decimal_places=2, label="工资")
    r_salary = forms.DecimalField(max_digits=5, decimal_places=2, label="请再输入工资")

    def clean_name(self):  # 全局钩子
        val = self.cleaned_data.get("name")
        if val.isdigit():
            raise ValidationError("用户名不能是纯数字")
        elif models.Emp.objects.filter(name=val):
            raise ValidationError("用户已经存在！！！！")
        else:
            return val

    def clean(self):  # 全局钩子，确认两次输入的工资是否一致
        val = self.cleaned_data.get("salary")
        r_val = self.cleaned_data.get("r_salary")
        if val == r_val:
            return self.cleaned_data
        else:
            raise ValidationError("请确认工资是否一致!!!")

    def clean_age(self):
        val = self.cleaned_data.get("age")
        if int(val) > 100 or int(val) <= 0:
            raise ValidationError('年龄不能大于100或者小于等于0')
        else:
            return val
